<?php
session_start();
$title="แสดงประวัติการเข้าระบบ";
include ('header-admin.php');

$page = (isset($_GET['page']))? intval($_GET['page']) : 1;
$limit_end = 25;
$limit_start = ($page-1)*$limit_end;

function page_navi($page, $limit, $range, $count, $send=NULL, $current_style="navi_on", $other_style="navi_out", $target="_self"){
    $output = "";
    $total = ceil($count/$limit);
    $navi_start = $page-$range;
    $navi_end = $page+$range;
     
    $send .= (!empty($send))? "&" : NULL;
         
    if($navi_start <= 0) $navi_start = 1;
    if($navi_end >= $total) $navi_end = $total;
	$type = $_GET['type'];
if(!isset($_GET['type'])){
	$type="1";
}
     
    if($page>1){
        $navi_back = $page-1;
        if($page > 2)
        $output .= "<a href=\"?" . $send . "type=".$type."&page=1\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>&laquo;</strong></a> ";
        $output .= "<a href=\"?" . $send . "type=".$type."&page=" . $navi_back . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>&#8249;</strong></a> ";
    }
    for($i = $navi_start; $i <= $navi_end; $i++){
        if($i == $page)
        $output .= "<a href=\"?" . $send . "type=".$type."&page=" . $i . "\" target=\"" . $target . "\" class=\"" . $current_style . "\"><strong><font color=\"" . $other_style . "\">$i</font></strong></a> ";
        else
        $output .= "<a href=\"?" . $send . "type=".$type."&page=" . $i . "\" target=\"" . $target . "\" class=\"" . $other_style . "\">$i</a> ";
    }
    if($page < $total){
        $navi_next = $page+1;
        $output .= "<a href=\"?" . $send . "type=".$type."&page=" . $navi_next . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>&#8250;</strong></a> ";
        if(($page+1) < $total)
        $output .= "<a href=\"?" . $send . "type=".$type."&page=" . $total . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>&raquo;</strong></a>";
    }
    if($navi_start>$navi_end)
        $output .= "<a href=\"?" . $send . "type=".$type."&page=" . $page . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>$page</strong></a> ";
    return $output;
}

$userID=$_SESSION["HBD_USER"];
$countTopic=0;
if($userID == 1){
?>
<script language="javascript">
	function ConfirmChoice(id){
	answer = confirm("คุณแน่ใจว่าจะลบ ?")
		if (answer !=0)
			location = "doDeleteUser.php?userlist_id=" + id;
	}
</script>
<?}?>
<div id="center-block" class="main-content">
<img src="img/register.png"/ id="img-block-content">
<div id="content-title-right">
		<h2 class="caption-header">แสดงประวัติการเข้าระบบ</h2>
		<span class="caption">แสดงประวัติการเข้าระบบทั้งหมดที่ผ่านมา</span>
		<span class="right-button">
		<!--form method="POST" action="addUser.php"><input type="submit" value="เพิ่มผู้ใช้" class="button"></form-->
				</span>
	</div>

	<hr>
	<table summary="webboard about Datastrucutre learning" id="table">
		<thead>
		<tr class="odd">
			<th scope="col" abbr="reserv_id">ID</th>
			<th scope="col" abbr="reserv_begin">ผู้ใช้</th>	
			<th scope="col" abbr="reserv_end">IP Address</th>
			<th scope="col" abbr="room_id">User Agent</th>
			<th scope="col" abbr="room_id">วันที่</th>
		</tr>	
		</thead>
		<tbody>
		<?
	$sql="SELECT historylog_id,userlist_username,historylog_ip,historylog_useragent,historylog_date FROM `historylog`,`userlist` WHERE userlist.userlist_id = historylog.userlist_id ORDER BY historylog_id DESC";
	$result = mysql_query($sql. " LIMIT " . $limit_start. "," . $limit_end);
	$count = mysql_num_rows($result);
	$total = mysql_num_rows(mysql_query($sql));
	if($total > 0){
	while ($rs = mysql_fetch_array($result)){
		$useragentIcon = $rs[historylog_useragent].' <img style="vertical-align: middle" src="./img/useragent_other.png" width="24" height="24"></img>';
		$countTopic++;
		if($countTopic%2==0)
			echo '<tr class="odd">';
		else
			echo '<tr class="even">';
		echo '<td>' . $rs[historylog_id] . '</a>';
		$rs[ReplyCount]--;
		echo '</td>';
		echo '<td>' . $rs[userlist_username] . '</td>';
		echo '<td>' . $rs[historylog_ip] . '</td>';
		if(preg_match('[.*(Firefox).*]',$rs[historylog_useragent])){
			$useragentIcon = $rs[historylog_useragent].' <img style="vertical-align: middle" src="./img/useragent_firefox.png" width="24" height="24"></img>';
		}
		if(preg_match('[.*(Safari).*]',$rs[historylog_useragent])){
			$useragentIcon = $rs[historylog_useragent].' <img style="vertical-align: middle" src="./img/useragent_safari_browser.png" width="24" height="24"></img>';
		}
		if(preg_match('[.*(IE).*]',$rs[historylog_useragent])){
			$useragentIcon = $rs[historylog_useragent].' <img style="vertical-align: middle" src="./img/useragent_internet_explorer.png" width="24" height="24"></img>';
		}
		echo '<td>' .$useragentIcon . '</td>';
		echo '<td>' . $rs[historylog_date] . '</td>';
	}
	echo "\n<div style=\"float:left;\">Page : " . page_navi($page, $limit_end, 4, $total, $send) . " </div><div style=\"float:right;\">[" . ($limit_start+1) . "-" . ($limit_start+$count) . "] of <strong>" . $total . "</strong></div>";
}
	?>
		</tbody>

	</table>
	</div>

<?
include ('footer.php');
?>